package com.example.siyun.util;

import lombok.extern.slf4j.Slf4j;
import org.csource.common.MyException;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
@Slf4j
public class FastDFSUtils {
    public static String[] upload(byte[] fileBytes){
        try {
//            ClientGlobal.initByProperties("config.properties");
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);//初始化
            TrackerClient trackerClient = new TrackerClient();//创建tracker
            TrackerServer trackerServer = trackerClient.getConnection();//获取tracker服务
            StorageServer storageServer = null;
            StorageClient storageClient = new StorageClient(trackerServer,storageServer);//存储连接
            String[] results = storageClient.upload_file(fileBytes,"MP4",null);//上传返回结果
            for (String str : results){
                System.out.println(str);
            }
            log.info("上传文件服务器成功");
            //前面提到的返回的group1
            //M00/00/00/rBL5_1vZTlGANIJPAABqgmoDdSo292.jpg
            return results;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        return null;
    }

}
